﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace PhotoNamingService
{
    public class IndexConfig
    {
        private static string IniFileName = "D:\\IndexConfig.ini";

        private const string IniName = "IndexConfig.ini";

        public static void CheckUpdateTime()
        {
            string lastDate = GetLastUpdateTime();
            string nowDate = DateTime.Now.ToString("yyMMdd");
            if (!lastDate.Equals(nowDate))
            {
                SetPhotoNameIndex(0);
                SetYWBHIndex(0);
                UpdateTime();
            }
        }

        public static int GetPhotoNameIndex()
        {
            CheckUpdateTime();
            string strIndex = IniValue("PhotoName", "Index");
            int index = 0;
            if (!int.TryParse(strIndex, out index))
            {
                string error = string.Format("无法解析的序列: {0}", strIndex);
                throw new Exception(error);
            }
            if (index < 0 || index >= 999)
            {
                string error = string.Format("序列号出现异常: {0}", index);
                throw new Exception(error);
            }
            else
            {
                SetPhotoNameIndex(++index);
                return index;
            }
        }

        private static void SetPhotoNameIndex(int index)
        {
            WriteValue("PhotoName", "Index", index.ToString());
            UpdateTime();
        }

        public static int GetYWBHIndex()
        {
            CheckUpdateTime();
            string strIndex = IniValue("YWBH", "Index");
            int index = 0;
            if (!int.TryParse(strIndex, out index))
            {
                string error = string.Format("无法解析的序列: {0}", strIndex);
                throw new Exception(error);
            }
            if (index < 0 || index >= 999)
            {
                string error = string.Format("序列号出现异常: {0}", index);
                throw new Exception(error);
            }
            else
            {
                SetYWBHIndex(++index);
                return index;
            }
        }


        private static void SetYWBHIndex(int index)
        {
            WriteValue("YWBH", "Index", index.ToString());
            UpdateTime();
        }

        public static string GetLastUpdateTime()
        {
            return IniValue("Update", "LastDate");
        }

        public static void UpdateTime()
        {
            WriteValue("Update", "LastDate", DateTime.Now.ToString("yyMMdd"));
        }

        /// <summary>
        /// 检查默认的配置文件是否存在，不存在则创建
        /// </summary>
        public static void CheckAndCreatConfigFile()
        {
            if (File.Exists(IniFileName))
            {
                return;
            }
            if (File.Exists("D:\\"))
            {
                IniFileName = "D:\\" + IniName;
                CreatDefaultIniFile();
                return;
            }
            if (File.Exists("C:\\"))
            {
                IniFileName = "C:\\" + IniName;
                CreatDefaultIniFile();
                return;
            }          
        }

        private static void CreatDefaultIniFile()
        {          
            SetPhotoNameIndex(0);
            SetYWBHIndex(0);
            FileInfo info = new FileInfo(IniFileName);
            if (info.Exists)
            {
                info.Attributes = FileAttributes.Hidden;
            }
        }
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string Section, string IniKey)
        {
            if (!File.Exists(IniFileName))
            {
                CheckAndCreatConfigFile();
                return "";
            }                     
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, IniKey, "", temp, 500, IniFileName);
            return temp.ToString();
        }
       
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static long WriteValue(string Section, string IniKey, string NewValue)
        {
            if (!File.Exists(IniFileName))
            {
                CheckAndCreatConfigFile();
            }           
            long i = WritePrivateProfileString(Section, IniKey, NewValue, IniFileName);
            return i;
        }

        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}